package ${package.Controller};

import com.amethyst.mybatisplus.common.ResponseResult;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
import io.swagger.annotations.Api;
import io.swagger.annotations.*;
import java.util.*;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${table.entityName};
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @date ${date}
 */
@Api(value = "${table.entityPath}接口")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/${table.entityPath}s")
@CrossOrigin
@RequiredArgsConstructor
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    private final ${table.serviceName} ${table.entityPath}Service;

    @ApiOperation(value = "新增${table.entityPath}信息",notes="新增${table.entityPath}信息")
    @ApiImplicitParam(name = "${table.entityPath}", value = "${table.entityPath}实体中的参数", required = true, dataType = "${entity}")
    @PostMapping("")
    @SneakyThrows(Exception.class)
    public ResponseResult<Void> add(@RequestBody ${entity} ${table.entityPath}) {
        ${table.entityPath}Service.save(${table.entityPath});
        return ResponseResult.ok();
    }

    @ApiOperation(value = "更新${table.entityPath}信息",notes="更新${table.name}信息")
    @ApiImplicitParam(name = "${table.entityPath}", value = "${table.entityPath}实体", required = true, dataType = "${entity}")
    @PutMapping("")
    @SneakyThrows(Exception.class)
    public ResponseResult<Void> update(@RequestBody ${entity} ${table.entityPath}) {
        ${table.entityPath}Service.updateById(${table.entityPath});
        return ResponseResult.ok();
    }

    @ApiOperation(value = "根据id获取${table.entityPath}",notes="根据id获取${table.entityPath}")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "<#list table.fields as field><#if field.keyFlag == true>${field.columnType?lower_case?cap_first}</#if></#list>")
    @GetMapping("/{id}")
    @SneakyThrows(Exception.class)
    public ResponseResult<${entity}> get(@PathVariable(value = "id") <#list table.fields as field><#if field.keyFlag == true>${field
            .columnType?lower_case?cap_first}</#if></#list> id) {
        return ResponseResult.ok(${table.entityPath}Service.getById(id));
    }

    @ApiOperation(value = "根据id删除${table.entityPath}",notes="根据id删除${table.entityPath}")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "<#list table.fields as field><#if field.keyFlag == true>${field.columnType?lower_case?cap_first}</#if></#list>")
    @DeleteMapping("/{id}")
    @SneakyThrows(Exception.class)
    public ResponseResult<Void> delete(@PathVariable(name = "id") <#list table.fields as field><#if field.keyFlag == true>${field.columnType?lower_case?cap_first}</#if></#list> id) {
        ${table.entityPath}Service.removeById(id);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "获取所有${table.entityPath}信息",notes="获取所有${table.entityPath}信息")
    @ApiImplicitParam()
    @GetMapping("/all")
    @SneakyThrows(Exception.class)
    public ResponseResult<List<${entity}>> listAll() {
        return ResponseResult.ok(${table.entityPath}Service.list());
    }

    @ApiOperation(value = "分页获取${table.entityPath}列表",notes="分页获取${table.entityPath}列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页数据条数", dataType = "Integer"),
    })
    @GetMapping("")
    @SneakyThrows(Exception.class)
    public ResponseResult<Page<${entity}>> listByPage(@RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
        @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ResponseResult.ok(${table.entityPath}Service.page(new Page<>(pageNo, pageSize), Wrappers.emptyWrapper()));
    }
}
</#if>
